<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改主日敬拜')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-sundayworship-edit" th:object="${bntSundayworship}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">敬拜日期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="woshipDate" th:value="${#dates.format(bntSundayworship.woshipDate, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">敬拜类型：</label>
                <div class="col-sm-8">
                    <select name="worshipType" class="form-control m-b" th:with="type=${@dict.getType('bnt_sunday_worship_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{worshipType}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">第几堂：</label>
                <div class="col-sm-8">
                    <input name="worshipIndex" th:field="*{worshipIndex}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">主题：</label>
                <div class="col-sm-8">
                    <input name="title" th:field="*{title}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">详细介绍，如主礼/证道等：</label>
                <div class="col-sm-8">
                    <textarea name="detail" class="form-control">[[*{detail}]]</textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">浏览次数：</label>
                <div class="col-sm-8">
                    <input name="visitNum" th:field="*{visitNum}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">证道：</label>
                <div class="col-sm-8">
                    <input id="sermon_text" name="sermon" th:field="*{sermon}" class="form-control" type="hidden">
                    <div class="ibox-content no-padding">
                        <div id="sermon_div" class="summernote">
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">视频上传：</label>
                <div class="col-sm-8">
                    <div class="file-loading">
                        <input id="video_input" class="file" type="file" multiple data-min-file-count="1" data-theme="fas">
                    </div>
                    <input id="video_name_input" name="videoName" th:field="*{videoName}" class="form-control" type="hidden">
                    <input id="video_url_input" name="videoUrl" th:field="*{videoUrl}" class="form-control" type="hidden">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input name="remark" th:field="*{remark}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('sys_notice_status')}">
                        <input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}" th:field="*{state}">
                        <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script th:inline="javascript">
        $(function() {
            $('.summernote').summernote({
                placeholder: '请输入证道内容',
                height : 192,
                lang : 'zh-CN',
                followingToolbar: false,
                callbacks: {
                    onImageUpload: function (files) {
                        sendFile(files[0], this);
                    }
                }
            });
            var content = $("#sermon_text").val();
            $('#sermon_div').summernote('code', content);
            var fileUrl = $("#video_url_input").val().split("?");
            var fileName = $("#video_name_input").val().split("?");
            var ft = [];
            for(var i=0;i<fileUrl.length;i++){
                vedioType = fileName[i].substr(fileName[i].lastIndexOf(".")+1);
                ft.push({
                    type:"video",
                    filetype:'video/' + vedioType,
                    key:fileName[i]
                });
            }
            $("#video_input").fileinput('destroy');
            $("#video_input").fileinput({
                language: 'zh', //设置语言
                uploadUrl: ctx + "common/uploads",
                dropZoneTitle: '请选择将要上传的视频',
                autoReplace: true,
                allowedFileExtensions: ["mp4", "avi", "dat", "3gp", "mov", "rmvb"],
                minFileCount: 1,
                maxFileCount: 1,
                previewFileType: ['video'],
                fileType: "video",
                allowedPreviewTypes: ['video'],
                previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
                initialPreviewAsData: true,
                initialPreviewFileType: 'video',
                initialPreview:fileUrl,
                initialPreviewConfig: ft,
                initialPreviewShowDelete:true,
                deleteUrl:ctx + "common/deleteFile",
                overwriteInitial: false
            });


            $("#video_input").on("fileuploaded", function(event, data, previewId, index) {
                var fn = $("#video_name_input").val();
                var fu = $("#video_url_input").val();
                if(fn == null || fn.length == 0){
                    fn = data.response.fileName;
                }else{
                    fn += "?" + data.response.fileName;
                }
                if(fu == null || fu.length == 0){
                    fu = data.response.url;
                }else{
                    fu += "?" + data.response.url;
                }
                $("#video_name_input").val(fn);
                $("#video_url_input").val(fu);
            });

            $("#video_input").on("filesuccessremove", function (event, data, previewId, index) {
                var fn = $("#video_name_input").val();
                var fu = $("#video_url_input").val();
                var fns = fn.split("?");
                var fus = fu.split("?");
                fns.splice(previewId,1);
                fus.splice(previewId,1);
                $("#video_name_input").val(fns.join("?"));
                $("#video_url_input").val(fus.join("?"));
            });
            $('#video_input').on('filedeleted', function(event, data, previewId, index) {
                var fn = $("#video_name_input").val();
                var fu = $("#video_url_input").val();
                var fns = fn.split("?");
                var fus = fu.split("?");
                for(var i=0;i<fns.length;i++){
                    if(fns[i] == data){
                        fns.splice(i,1);
                        fus.splice(i,1);
                    }
                }
                $("#video_name_input").val(fns.join("?"));
                $("#video_url_input").val(fus.join("?"));
            });
        });

        // 上传文件
        function sendFile(file, obj) {
            var data = new FormData();
            data.append("file", file);
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        $(obj).summernote('editor.insertImage', result.url, result.fileName);
                    } else {
                        $.modal.alertError(result.msg);
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            });
        }
        var prefix = ctx + "bnt/sundayworship";
        $("#form-sundayworship-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $("#sermon_text").val($("#sermon_div").summernote("code"));
                $.operate.save(prefix + "/edit", $('#form-sundayworship-edit').serialize());
            }
        }

        $("input[name='woshipDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
    </script>
</body>
</html>